//
//  CZVistorView.swift
//  Weibo
//
//  Created by 铭铭 on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
//按钮点击协议
protocol CZVistorViewDelegate: NSObjectProtocol{

    //点击注册
      func VistorViewRegisterClick()

    //点击登录
     func VistorViewLoginClick()
}


//首页的登陆视图
class CZVistorView: UIView {
    
    // 按钮点击代理属性
    weak var VistorViewDelegate :CZVistorViewDelegate?

    
    // swift默认所有的view都能通过xib/storyboard 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //初始化UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置视图背景
      backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
         prepareUI()
    }
    
    //房子动画暂停旋转
    func pauseAnimation() {
        
        //纪录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
     
        // 设置动画速度
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    
    }
    //房子动画恢复旋转
    func resumeAnimation(){
    
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
    
        // 设置动画速度
        iconView.layer.speed = 1
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = 0
        
        //重置开始时间
        iconView.layer.beginTime = 0
        
        //计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    
    //使轮子转起来
    func startRotationAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.removedOnCompletion = false
        
      iconView.layer.addAnimation(animation, forKey: "homeRotation")
    
    }
    
    // MARK: - 点击事件
    func vistorViewRegister() {
    
        VistorViewDelegate?.VistorViewRegisterClick()
    }
    //
    func vistorViewLogin() {
        
        VistorViewDelegate?.VistorViewLoginClick()
       
    }
    
    /* 不同页面设置不同信息
    默认为首页,首页不需要调用
    - parameter imageName: 图标名称
    - parameter message:   消息文字
    */
    func setupInfo(imageName:String,message:String){
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        // 隐藏小房子
        homeView.hidden = true
        
        sendSubviewToBack(coverView)
    }
    
    
    //准备登陆UI
    private func prepareUI(){
       
        
        // 1 添加子控件
        addSubview(iconView)
        //添加遮罩视图
        addSubview(coverView)
        
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //关闭Autoresizing,不让它干扰autolayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 创建约束对象
          /// iconView CenterX与父控件的CenterX重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        /// iconView CenterY与父控件的CenterY重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 小房子
        // x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label
        // x
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // y
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽度约束
        // 当 toItem = nil时attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        // 注册按钮
        // 左边和label对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        // 高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右边和label对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        // 高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置遮罩约束
        //X
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //Y
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 底部和registerButton底部重合
         addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
       
       
    }
    
    
    //按钮懒加载
    //转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        // 设置label文本了
        label.text = "关注一些人,看看有神马惊喜"
        label.numberOfLines = 0
        
        // 根据文本来适应大小
        label.sizeToFit()
        
        return label
        }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        
        // 设置文本颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 根据内容适应大小
        button.sizeToFit()
        // 添加点击事件
        button.addTarget(self, action: "vistorViewRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        // 设置文本颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 根据内容适应大小
        button.sizeToFit()
        
        // 添加点击事件
        button.addTarget(self, action: "vistorViewLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()

    // 懒加载，添加遮罩视图
   private lazy var coverView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
    
        return UIImageView(image: image)
    }()
    
    
    
}
